package cn.liangcaomei.eduservice.service.impl;

import cn.liangcaomei.eduservice.entity.EduSubject;
import cn.liangcaomei.eduservice.entity.excel.SubjectData;
import cn.liangcaomei.eduservice.entity.subject.OneSubject;
import cn.liangcaomei.eduservice.entity.subject.TwoSubject;
import cn.liangcaomei.eduservice.listener.SubjectExcelListener;
import cn.liangcaomei.eduservice.mapper.EduSubjectMapper;
import cn.liangcaomei.eduservice.service.EduSubjectService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author LiuJie
 * @since 2024-01-25
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加課程分類
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            //文件輸入流
            InputStream in = file.getInputStream();
            //調用方法
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查詢課程分類列表方法（樹形）
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1  查詢所有的一級分類
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectsList = baseMapper.selectList(wrapperOne);

        //2  查詢所有的二級分類
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectsList = baseMapper.selectList(wrapperTwo);
        //System.out.println(twoSubjectsList);

        //創建list集合，用於存儲最終的封裝數據
        List<OneSubject> finalSubjectList = new ArrayList<>();
        //3  封裝一級分類
        for (int i = 0; i < oneSubjectsList.size(); i++) {
            //得到 oneSubjectsList 的每個 EduSubject 對象
            EduSubject eduSubject = oneSubjectsList.get(i);
            //把 eduSubject 裏面的數據，放到 OneSubject 對象中
            OneSubject oneSubject = new OneSubject();
            //第一種
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            //第二種：用工具類實現
            BeanUtils.copyProperties(eduSubject,oneSubject);
            finalSubjectList.add(oneSubject);

            //4  封裝二級分類
            //在一級分類循環中遍歷二級分類記錄
            //創建list集合封裝每一個一級分類中的二級分類
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            //遍歷二級分類 list 集合
            for (int j = 0; j < twoSubjectsList.size(); j++) {
                //獲取每一個二級分類
                EduSubject twoSubject = twoSubjectsList.get(j);
                //判斷二級分類的 parent_id 和一級分類的 id 是否一樣
                //System.out.println("=============== 進入二級=============== ");
                //System.out.println(twoSubject);
                if (twoSubject.getParentId().equals(oneSubject.getId())) {
                    //把 oneSubject 的值copy到 twoFinalSubjectList
                    TwoSubject tSubject = new TwoSubject();
                    BeanUtils.copyProperties(twoSubject,tSubject);
                    twoFinalSubjectList.add(tSubject);
                }
            }
            //把一級下面的所有二級分類放到一級分類中
            oneSubject.setChildren(twoFinalSubjectList);
        }

        return finalSubjectList;
    }
}
